/*
 * Activiti Modeler component part of the Activiti project
 * Copyright 2005-2014 Alfresco Software, Ltd. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

/*
 * Condition expression
 */

var KisBpmConditionExpressionCtrl = [ '$scope', '$modal', function($scope, $modal) {

    // Config for the modal window
    var opts = {
        template:  'editor-app/configuration/properties/condition-expression-popup.html?version=' + Date.now(),
        scope: $scope
    };

    // Open the dialog
    $modal(opts);
}];

var KisBpmConditionExpressionPopupCtrl = [ '$scope', '$translate', '$http', function($scope, $translate, $http) {

    // 关闭
    $scope.close = function() {
        handleAssignmentInput($scope);
        $scope.property.mode = 'read';
        $scope.$hide();
    };
    console.log($scope.property);
    // Put json representing assignment on scope(如果分配用户的值全都有值)
    if ($scope.property.value !== undefined && $scope.property.value !== null)
    {
        //赋值
        $scope.Fieldconditions = $scope.property.value;
    } else {
        $scope.Fieldconditions = {};
    }

    //弹出(选择)代理人
    $scope.onChangeUser = function (index) {
        //封装一个深克隆函数方法deepClone
        function deepClone(o) {
            if(
                typeof o=="string"||
                typeof o=="number"||
                typeof o=="undefined"||
                typeof o=="boolean"
            ){
                return o;
            }else if(Array.isArray(o)){
                var arr=[];
                for (var i=0;i<o.length;i++){
                    arr.push(deepClone(o[i]));
                }
                return _arr;
            }else if(typeof 0=="object"){
                var _o={};
                for(var k in o){
                    _o[k]=deepClone(o[k]);
                }
                return _o
            }
        }

        //获取workflow上的textarea的id
        var textarea3=document.getElementById("textareadata");
        if(textarea3.value){
            var textarea4=textarea3.value;//textarea上的值
            console.log(textarea4);//必须英文字段
            textarea8= encodeURIComponent(deepClone(textarea4));//因为textarea英文字段时其中一个参数
            console.log(textarea8);


            var reg=/[^\{}]*\{(.*)\}[^\}]*/;//正则方法取{里面的值}
            textarea5=textarea4.replace(reg,'$1');//首先获取{}里面的值
            console.log(textarea5)
            var reg2=/(?<=\$).*?(?=\$)/gi; //正则方法取$包裹的值
            textarea6=textarea5.match(reg2);//分成一个个字段
            console.log(textarea6);

            //准备过滤刚刚正则方法分出来的字段数组去掉那些符号
            // var textarea7=textarea6.filter(item=>item!="+"&&item!="-"&&item!="×"&&item!="÷"&&item!=">"&&item!="<"&&item!="="&&item!="&&"&&item!="||"&&item!="!"&&item!="("&&item!=")");
            var textarea7=textarea6.filter(item => !(item.indexOf("||")!==-1 || item.indexOf("&&") !==-1||item.indexOf("+") !==-1||item.indexOf("-") !==-1||item.indexOf("<") !==-1||item.indexOf(">") !==-1||item.indexOf("=") !==-1||item.indexOf("×") !==-1||item.indexOf("÷") !==-1||item.indexOf("！") !==-1||item.indexOf("(") !==-1||item.indexOf(")") !==-1));
            console.log(textarea7);
            var cTableName=JSON.parse(localStorage.getItem("cTableName"));
            console.log(cTableName);//获得cTableName
            var data5=JSON.parse(localStorage.getItem("datayl")).formItem;
            console.log(data5);  //获得数组准备做比较字段
            var data5arr=textarea4.match(/[^\[]+(?=\])/g);

            //获取iframe插入页面的textarea的id
            iddd=document.getElementById("iframe").contentWindow.document.getElementById("textarea");
            console.log(iddd);
            //这里封装原生ajax参数变成字符串方法
            window.queryString=function (json) {
                var arr=[];
                for(k in json){
                    arr.push(k+"="+json[k]);
                }
                return arr.join("&");
            }

            console.log(textarea8);
            console.log(textarea4);

            //从这里开始原生的ajax方法
            if(window.XMLHttpRequest){
                var xhr=new XMLHttpRequest;
            }else{
                var xhr=new ActiveXObject("msxml2.xmlhttp");
            }
            xhr.open("post","../../../../../../../DSMP/formltems/showElString.do",true);
            xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
            xhr.send(queryString({
                cTableName:cTableName,
                elString:textarea8,
                fields:textarea7
            }))
            xhr.onreadystatechange=function (argument) {
                if(xhr.status==200||xhr.readyState==4){
                    console.log(xhr.responseText);
                    var reg=/[^\{}]*\{(.*)\}[^\}]*/;
                    jieguo1=xhr.responseText.replace(reg,'$1');//首先获取{}里面的值
                    console.log(jieguo1);
                    jieguo2=jieguo1.replace(reg,'$1');

                    var reg2=/\$.*?\$/gi;

                    jieguo3=jieguo2.replace(reg2,(val) =>{
                        return "["+val.slice(1,-1)+"]"
                    })
                    console.log(jieguo3);
                    iddd.value=jieguo3;//赋值方法
                }
            }
        }



        CandidateGroupsIndex = index;
        document.getElementById('changeMenTree1').style.display = "block";


    };

    //保存
    $scope.save2 = function() {

        //申请负责人赋值
        console.log($scope.property);

        // if(!$scope.assignment.Principalinfo_id){
        // 	$scope.assignment.assignee=$scope.data.Principalinfo_id;
        // }

        //代理人赋值++

        if(!$scope.Fieldconditions){
            $scope.Fieldconditions =  $scope.Fieldconditions;
        }
        console.log($scope.Fieldconditions,'保存+++++++++++++++++');
        console.log( $scope.property);
        $scope.property.value = $scope.Fieldconditions;
        //

        $scope.updatePropertyInModel($scope.property);
        console.log( $scope.property);
        $scope.close();
        // console.log($scope.property.value );
    };

    $scope.closeChangeUser = function(action) {
        if(action == 'cancel' || action == 'close'){
            //关闭弹出框（隐藏）
            document.getElementById('changeMenTree1').style.display = "none";
        }else{
            //赋值（iframe父页面调用子页面的方法）
            var iframe = window.frames["mini-iframe-666"].onOk();
            console.log(iframe);
            iframe.newStrEn="$"+"{"+  iframe.newStrEn+"}";
            if(iframe){
                //人员ID(保存时保存的值)
                $scope.Fieldconditions = iframe.newStrEn.replace(/\[|]/g,'$');
                // $scope.Fieldconditions = iframe.newStrEn;
                //传值
                // console.log(iframe.PersonnelFiles_id);
                // var Pid = iframe.PersonnelFiles_id;
                // localStorage.setItem("Pid",Pid);
                //弹出框隐藏

                console.log(iframe,'候选组');
                $scope.Fieldconditions [CandidateGroupsIndex] = iframe;
                console.log(iframe.textarea );
                // window.location.href="../properties/condition-expression-popup.html";
                localStorage.setItem("iframedata",JSON.stringify(iframe ));
                document.getElementById('changeMenTree1').style.display = "none";
            }else{
                $scope.Fieldconditions ='';
                document.getElementById('changeMenTree1').style.display = "none";
            }
        }
    };

    // Put json representing condition on scope
    if ($scope.property.value !== undefined && $scope.property.value !== null) {

        $scope.conditionExpression = {value: $scope.property.value};

    } else {
        $scope.conditionExpression = {value: ''};
    }

    $scope.save = function() {
        $scope.property.value = $scope.conditionExpression.value;
        $scope.updatePropertyInModel($scope.property);
        $scope.close();
    };

    // Close button handler
    $scope.close = function() {
        $scope.property.mode = 'read';
        $scope.$hide();
    };
}];